package com.junchenrobot.mes.core;

import com.junchenrobot.mes.client.KCClient;
import com.junchenrobot.mes.dao.DeviceDao;
import com.junchenrobot.mes.dao.WinderDao;
import com.junchenrobot.mes.dao.WinderTaskDao;
import com.junchenrobot.mes.domain.WinderTask;
import com.junchenrobot.mes.dto.param.TaskParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 装载任务处理器
 *
 * @author SeaWave
 */
@Component
public class InputTaskHandler {
    
    private final WinderTaskDao winderTaskDao;
    
    private final KCClient kcClient;
    
    private final WinderDao winderDao;
    
    private final DeviceDao deviceDao;
    
    @Autowired
    public InputTaskHandler(WinderTaskDao winderTaskDao, KCClient kcClient, WinderDao winderDao,
                            DeviceDao deviceDao) {
        this.winderTaskDao = winderTaskDao;
        this.kcClient = kcClient;
        this.winderDao = winderDao;
        this.deviceDao = deviceDao;
    }
    
    // public WinderTask receive(AgvWork agvWork) {
    //
    //     Integer line = agvWork.getLine();
    //     Integer spinNo = agvWork.getSpinNo();
    //     Winder winder = winderDao.listPosition(line.toString(), spinNo.toString());
    //
    //     // 获取车的状态
    //     Device device = deviceDao.getDevice("1");
    //     // 车名称
    //     String carName = device.getDeviceName();
    //
    //     boolean aNull = Obj
    //     ects.isNull(winder);
    //
    //     String location = "";
    //
    //     Integer deviceStatus = device.getDevicePole();
    //
    //
    //     int poleName = 0;
    //     // 0空闲 1 左杆有货 2右杆有货 3满货
    //     //  默认1杆
    //     if (deviceStatus == 0) {
    //         poleName = 1;
    //     } else if (deviceStatus == 1) {
    //         poleName = 2;
    //     } else if (deviceStatus == 2) {
    //         poleName = 1;
    //     } else if (deviceStatus == 3) {
    //         // 满货返回
    //         return WinderTask.builder().taskExecuteStatus(2).build();
    //     }
    //
    //     if (poleName == 1) {
    //         location = winder.getWinderLeftPosition();
    //     }
    //     if (poleName == 2) {
    //         location = winder.getWinderRightPosition();
    //     }
    //
    //     // 更新那个杆有货
    //     // deviceDao.updateDevice(build.getTaskDeviceName(), poleName);
    //     // winderTaskDao.insertNewTask(build);
    //     return build;
    // }
    
    public void newTask(WinderTask winderTask) {
        Integer inTaskExecuteStatus = winderTask.getTaskExecuteStatus();
        if (inTaskExecuteStatus == -1) {
            TaskParam taskParam = new TaskParam();
            if (winderTask.getTaskType() == 1) {
                taskParam.setLocation(winderTask.getTaskLocation());
                taskParam.setTaskType("取料");
                taskParam.setLoadDirection(winderTask.getTaskPoleCode());
            } else {
                taskParam.setTaskType("放料");
                taskParam.setLocation("1");
                taskParam.setUnLoadDirection(winderTask.getTaskPoleCode());
            }
            taskParam.setPriority(0);
            taskParam.setVehicleNo(winderTask.getTaskDeviceName());
            taskParam.setFinishAction(0);
            taskParam.setTaskCode(winderTask.getTaskCode());
            kcClient.addRegularTask(taskParam);
        }
    }
}
